define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'order/index' + location.search,
                    add_url: 'order/add',
                    // edit_url: 'order/edit',
                    del_url: 'order/del',
                    multi_url: 'order/multi',
                    import_url: 'order/import',
                    table: 'order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        { field: 'sn', title: __('Sn'), operate: 'LIKE' },
                        { field: 'user.nickname', title: __('User.nickname'), operate: 'LIKE' },
                        // { field: 'total_money', title: __('Total_money'), operate: 'BETWEEN' },
                        { field: 'pay_money', title: __('Pay_money'), operate: 'BETWEEN' },
                        { field: 'total', title: __('Total') },
                        { field: 'taketime', title: __('Taketime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'consignee', title: __('Consignee'), operate: 'LIKE' },
                        { field: 'phone', title: __('Phone'), operate: 'LIKE' },
                        { field: 'address', title: __('Address'), operate: 'LIKE' },
                        { field: 'pay_type', title: __('Pay_type'), searchList: { "1": __('Pay_type 1'), "2": __('Pay_type 2') }, formatter: Table.api.formatter.normal },
                        { field: 'express_com', title: __('Express_com'), operate: 'LIKE' },
                        { field: 'express_no', title: __('Express_no'), operate: 'LIKE' },
                        { field: 'total_points', title: __('Total_points'), operate: 'BETWEEN' },
                        { field: 'paytime', title: __('Paytime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'status', title: __('Status'), searchList: { "0": __('Status 0'), "1": __('Status 1'), "2": __('Status 2'), "3": __('Status 3'), "4": __('Status 4'), "-1": __('Status -1') }, formatter: Table.api.formatter.status },
                        // { field: 'canceltime', title: __('Canceltime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        { field: 'createtime', title: __('Createtime'), operate: 'RANGE', addclass: 'datetimerange', autocomplete: false, formatter: Table.api.formatter.datetime },
                        {
                            field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'deliver',
                                    title: '发货',
                                    text: '发货',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order/deliver',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.status == '1') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }
                                    },
                                },
                                {
                                    name: 'select',
                                    title: '查看商品',
                                    text: '查看商品',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'order.goods/index',//接口地址 控制器名/方法名
                                },
                                {
                                    name: 'query',
                                    title: '查看物流',
                                    text: '查看物流',
                                    classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                                    url: 'express/query?express_com={express_com}&express_no={express_no}',//接口地址 控制器名/方法名
                                    visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                                        if (row.status >= '2') {
                                            return true;//显示
                                        } else {
                                            return false;//隐藏
                                        }

                                    },
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        { checkbox: true },
                        { field: 'id', title: __('Id') },
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        deliver: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
